package service;

import dao.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import pojo.*;
import until.SqlSessionFactoryUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    //获取Session
//    SqlSession sqlSession = factory.openSession();
//    OrderMapper OrderMapper = sqlSession.getMapper(OrderMapper.class);

    public List<Order> selectOrder(String username){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        UserMapper mapper2 = sqlSession.getMapper(UserMapper.class);
        User user = mapper2.selectUserId(username);
        return mapper.selectAll(user.getId().toString());
    }
    public List<Order> selectOrder(){
        SqlSession sqlSession = factory.openSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        return mapper.selectAllOrder();
    }
    public List<pojo.OrderItem> selectOrderItem(String orderId){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        OrderItemMapper mapper = sqlSession.getMapper(OrderItemMapper.class);
        return mapper.selectAll(orderId);
    }
    public Book2 selectBookItem(long id){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        System.out.println("数据库查询前"+ LocalDateTime.now());
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        Book2 book2 = mapper.selectBookItem(id);
        System.out.println("数据库查询后"+ LocalDateTime.now());
        return book2;
    }

    public int insertOrder(long id,long userId,int totalCount,double totalAmout){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        //获取Mapper
        int i= mapper.insertOrder(id, userId, totalCount, totalAmout);
        sqlSession.commit();
        return i;
    }
    public int insertOrderItem(long id,long orderId,long bookId,int number,String bookName,double price){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        OrderItemMapper mapper = sqlSession.getMapper(OrderItemMapper.class);
        int i= mapper.insertOrderItem(id, orderId, bookId, number,bookName,price);
        sqlSession.commit();
        return i;
    }

    public long validateOrderId(long id){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        return mapper.validateOrderId(id);
    }
    public long validateOrderItemId(long id){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        OrderItemMapper mapper = sqlSession.getMapper(OrderItemMapper.class);
        return mapper.validateOrderItemId(id);
    }
    public int adminUpdateOrder(String id,String status){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        OrderItemMapper mapper = sqlSession.getMapper(OrderItemMapper.class);

        int i = mapper.adminUpdateOrder(id, status);
        sqlSession.commit();
        return i;
    }
    public int adminDeleteOrder(String id){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        OrderItemMapper mapper = sqlSession.getMapper(OrderItemMapper.class);
        int i = mapper.adminDeleteOrder(id);
        sqlSession.commit();
        return i;

    }

}
